package com.ruoyi.canteen.mapper;

import java.util.List;
import com.ruoyi.canteen.domain.CanteenUser;
import org.apache.ibatis.annotations.*;

/**
 * 点餐用户Mapper接口
 * 
 * @author zhangw
 * @date 2021-09-19
 */
public interface CanteenUserMapper 
{
    /**
     * 查询点餐用户
     * 
     * @param id 点餐用户主键
     * @return 点餐用户
     */
    public CanteenUser selectCanteenUserById(String id);

    /**
     * 查询点餐用户列表
     * 
     * @param canteenUser 点餐用户
     * @return 点餐用户集合
     */
    public List<CanteenUser> selectCanteenUserList(CanteenUser canteenUser);

    /**
     * 新增点餐用户
     * 
     * @param canteenUser 点餐用户
     * @return 结果
     */
    public int insertCanteenUser(CanteenUser canteenUser);

    /**
     * 修改点餐用户
     * 
     * @param canteenUser 点餐用户
     * @return 结果
     */
    public int updateCanteenUser(CanteenUser canteenUser);

    /**
     * 删除点餐用户
     * 
     * @param id 点餐用户主键
     * @return 结果
     */
    public int deleteCanteenUserById(String id);

    /**
     * 批量删除点餐用户
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCanteenUserByIds(String[] ids);

    @Select("SELECT id,openid,nickname,user_code,unify_uid,mobile,wx_profile,user_type from canteen_user WHERE openid=#{openid}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "openid", column = "openid"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "userCode", column = "user_code"),
            @Result(property = "unifyUid", column = "unify_uid"),
            @Result(property = "mobile", column = "mobile"),
            @Result(property = "wxProfile", column = "wx_profile"),
            @Result(property = "userType", column = "user_type")
    })
    public CanteenUser selectCanteenUserByOpenId(String openId);

    @Select("SELECT id,openid,nickname,user_code,unify_uid,mobile,wx_profile,user_type " +
            "from canteen_user WHERE user_code=#{userCode}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "openid", column = "openid"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "userCode", column = "user_code"),
            @Result(property = "unifyUid", column = "unify_uid"),
            @Result(property = "mobile", column = "mobile"),
            @Result(property = "wxProfile", column = "wx_profile"),
            @Result(property = "userType", column = "user_type")
    })
    public CanteenUser selectCanteenUserByUserCode(String userCode);

    /**
     * 通过openid，更新用户信息
     * @param canteenUser 微信用户信息
     * @return
     */
    @Update("UPDATE canteen_user SET nickname=#{nickname},mobile=#{mobile},wx_profile=#{wxProfile} " +
            "WHERE id=#{id}")
    public int updateUserWxInfo(CanteenUser canteenUser);

    /**
     * 新增企业点餐用户
     * 校验用户code 是否存在
     * @param canteenUser 点餐用户
     * @return 结果
     */
    @Insert("insert into canteen_user(nickname,user_code,mobile,wx_profile,user_type,create_time) " +
            "select #{nickname},#{userCode}, #{mobile},#{wxProfile},#{userType},#{createTime} from DUAL " +
            "where not EXISTS (select id from canteen_user where user_code = #{userCode})")
    @Options(keyColumn = "id", keyProperty = "id", useGeneratedKeys = true)
    public int insertQyCanteenUser(CanteenUser canteenUser);

}
